//  this library.
import 'package:flutter/material.dart';
import 'package:hktk_app/decoration/chapter_expansion_tile.dart';
import 'package:hktk_app/model/my/app_list_model.dart';
import 'package:hktk_app/util/font_util.dart';
import 'package:hktk_app/util/view_util.dart';

/// 问题可展开组件
class CustomerExpandableWidget extends StatefulWidget {
  /// 相关内容数据
  final AppListModel model;

  /// 对应的下标
  final int index;

  const CustomerExpandableWidget({
    super.key,
    required this.model,
    required this.index,
  });

  @override
  State<CustomerExpandableWidget> createState() =>
      _CustomerExpandableWidgetState();
}

class _CustomerExpandableWidgetState extends State<CustomerExpandableWidget> {
  /// 是否处于打开模式
  late bool _isOpenIt;

  @override
  void initState() {
    super.initState();
    debugPrint("customerExpandableWidget 组件 initState 方法执行 ===> ");
    debugPrint("");
    _isOpenIt = false;
  }

  @override
  Widget build(BuildContext context) {
    debugPrint("customerExpandableWidget 组件 build 方法执行 ===> ");
    debugPrint("");
    return ChapterExpansionTile(
      // 监听是否打开
      onExpansionChanged: _onChangedFun,
      // 父组件显示样式
      title: _title(),
      // 是否显示分割线
      dividerDisplayTime: DividerDisplayTime.never,
      // 子集组件
      children: _childrenItems(),
    );
  }

  /// 标题组件显示
  _title() {
    String title = "${widget.index}、${widget.model.name}";
    return SizedBox(
      height: 88,
      child: Row(
        children: [
          Expanded(
            child: Text(
              title,
              style: FontUtil.fontBlack1(20),
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
            ),
          ),
          Icon(
            _isOpenIt ? Icons.expand_more : Icons.chevron_right,
            size: 32,
          ),
          // SvgPicture.asset(
          //   "images/my/wrong_question_icon.svg",
          //   width: 8,
          //   height: px16,
          // ),
        ],
      ),
    );
  }

  /// 监听是否打开方法
  void _onChangedFun(bool value) {
    debugPrint("${widget.model.name} 是否打开 ===> $value");
    setState(() {
      _isOpenIt = value;
    });
  }

  /// 子集组件显示
  _childrenItems() {
    List<Widget> widgets = [];
    widgets.add(
      _indentItem(
        width: 572,
        child: Text(
          widget.model.answer ?? "暂无",
          style: FontUtil.fontGrey1(20),
        ),
      ),
    );
    widgets.add(_indentItem(height: 48));
    return widgets;
  }

  /// 缩进组件显示
  _indentItem({
    double? width,
    double? height,
    Widget? child,
  }) {
    return Container(
      width: width,
      height: height,
      margin: EdgeInsets.only(left: 58),
      child: child,
    );
  }
}
